<template>
  <el-container>
    <el-main>
      <div style="width: 100%;display: flex;align-items: flex-end;border-bottom: 1px #eee solid;margin-bottom: 20px">
        <span style="font-size: 18px;color:#409EFF;margin: 10px 0;">基本信息</span>
      </div>
      <el-form ref="sysMsgForm" :model="sysMsgForm" :rules="rules" label-width="80px">
        <el-col :span="14">
          <el-form-item label="消息标题">
            <el-input v-model="sysMsgForm.msgTitle"></el-input>
          </el-form-item>
          <el-form-item label="内容级别">
            <el-col :span="12">
              <el-select v-model="sysMsgForm.msgLevel" placeholder="请选择内容级别">
                <el-option label="普通" value="1"></el-option>
                <el-option label="一般" value="2"></el-option>
                <el-option label="紧急" value="3"></el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="内容类型">
            <el-col :span="12">
              <el-select v-model="sysMsgForm.msgType" placeholder="请选择内容类型">
                <el-option label="公告" value="1"></el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="接收者">
          <el-col :span="12">
            <el-select v-model="sysMsgForm.receiveType" placeholder="请选择接收者类型">
              <el-option label="全部" value="0"></el-option>
              <el-option label="用户" value="1"></el-option>
              <el-option label="部门" value="2"></el-option>
              <el-option label="角色" value="3"></el-option>
              <el-option label="岗位" value="4"></el-option>
            </el-select>
          </el-col>
        </el-form-item>
          <el-form-item label="状态">
            <el-col :span="12">
              <el-select v-model="sysMsgForm.status" placeholder="请选择接收者类型">
                <el-option label="草稿" value="0"></el-option>
                <el-option label="审核" value="1"></el-option>
                <el-option label="驳回" value="2"></el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="消息内容">
            <el-input type="textarea" v-model="sysMsgForm.msgContent"></el-input>
          </el-form-item>
          <el-form-item label="有无附件">
            <el-radio-group v-model="sysMsgForm.isAttac">
              <el-radio label="有" value="1"></el-radio>
              <el-radio label="无" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="saveSysMsg('sysMsgForm')">新建信息</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-main>
  </el-container>
</template>
<script>
  import {save} from '@/api/sysMsg'
  import { EventBus } from '@/api/eventBus'
  export default {
    name: 'sysMsg-add',
    data() {
      return {
        sysMsgForm: {
          msgTitle: '',
          msgLevel: '',
          msgType: '',
          msgContent: '',
          receiveType:'',
          status:'',
          isAttac: ''
        },
        clicked: false,
        rules: {
          msgTitle: [
            {required: true, message: '请输入消息标题', trigger: 'blur'},
            {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur'}
          ],
          msgLevel: [
            {required: true, message: '请选择消息级别', trigger: 'blur'}
          ],
          msgType: [
            {required: true, message: '请选择消息类型', trigger: 'blur'}
          ],
          msgContent: [
            {required: true, message: '请输入消息内容', trigger: 'blur'}
          ],
          receiveType:[
            {required: true, message: '请选择接收者类型', trigger: 'blur'}
          ],
          status:[
            {required: true, message: '请选择状态', trigger: 'blur'}
          ],
          isAttac: [
            {required: true, message: '请选择有无附件', trigger: 'blur'}
          ]
        },
      }
    },

    methods: {
      saveSysMsg(sysMsgForm) {
        this.$refs[sysMsgForm].validate((valid) => {
          if (valid) {//校验表单
            this.clicked = true
            save(this.sysMsgForm).then(res => {
              console.log(res)
              this.clicked = false
              this.$message({
                message: res.content,
                offset: 120,
                center: true
              });
              EventBus.$emit('sysMsg-add', res.data)
              this.$store.dispatch('tagsView/delView', this.$route).then(({visitedViews})=>{
                this.$router.push({
                  path: '/sysMsg/list'
                })
              })
            }).catch(err => {
              console.log(err)
              this.clicked = false
            })
          } else {
            console.log('error submit')
            return false
          }
        })
        console.log('submit!');
      }
    }
  }
</script>
